﻿
/*
NAILSUPERSTORE ® COMPANY
 
[HISTORY]
Created By  	: NGUYEN VAN TY
Created Date	: 12/22/2010
Modified By     :
Modified Date   :
Note            :

*/

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using CommonTagTime;
using CommonTagTime.ModelObject;
using CommonTagTime.Business;
namespace CommonTagTime.DataAccess
{
    public class daoDocumentProduct
    {
        public daoDocumentProduct()
        {

        } 
        public int Insert(DocumentProduct objData)
        {
            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[2];          
            try
            {
                para[0] = new SqlParameter(busDocumentProduct.SKU, SqlDbType.VarChar);
                para[0].Value = objData.SKU;
                para[1] = new SqlParameter(busDocumentProduct.DOCUMENTID, SqlDbType.Int);
                para[1].Value = objData.DocumentID;
                result = clsSQL.SPExecute("DocumentProduct_Insert", para);                
            }
            catch (Exception ex)
            {                
                clsFile.WriteLog("daoDocumentProduct", "Insert(DocumentProduct objData)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            return result;
        }
        public DocumentProductCollection GetBydocumentID(int documentID)
        {
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            SqlDataReader de;
            DocumentProductCollection item = new DocumentProductCollection();
            try
            {
                para[0] = new SqlParameter(busDocument.DOCUMENTID, SqlDbType.Int);
                para[0].Value = documentID;
                using (de = clsSQL.SPDataReader("DocumentProduct_GetByDocumentID", para))
                {
                    while (de.Read())
                    {
                        item.Add(Load(de));
                    }

                    de.Close();
                    de.Dispose();
                }
            }
            catch (Exception ex)
            {
                item = null;
                clsFile.WriteLog("daoDocument", "GetBydocumentID(int documentID)", ex.ToString());
            }
            finally
            {
                de = null;
                clsSQL.Dispose();
                clsSQL = null;
            }
            return item;
        }
        public DocumentProductCollection GetByProductID(int productID)
        {
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            SqlDataReader de;
            DocumentProductCollection item = new DocumentProductCollection();
            try
            {
                para[0] = new SqlParameter(busDocument.DOCUMENTID, SqlDbType.Int);
                para[0].Value = productID;
                using (de = clsSQL.SPDataReader("DocumentProduct_GetByProductID", para))
                {
                    while (de.Read())
                    {
                        item.Add(Load(de));
                    }

                    de.Close();
                    de.Dispose();
                }
            }
            catch (Exception ex)
            {
                item = null;
                clsFile.WriteLog("daoDocument", "GetByProductID(int productID)", ex.ToString());
            }
            finally
            {
                de = null;
                clsSQL.Dispose();
                clsSQL = null;
            }
            return item;
        }
        private DocumentProduct Load(IDataReader de)
        {
            DocumentProduct item = new DocumentProduct();
            item.DocumentID = clsConvert.DbToInt("DocumentID", de);
            item.ProductID = clsConvert.DbToInt("ProductID", de);
            item.SKU = clsConvert.DbToString("SKU", de);           
            return item;            
        }
        public bool OrderDown(DocumentProduct objData)
        {
            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[2];
            try
            {
                para[0] = new SqlParameter(busDocumentProduct.SKU, SqlDbType.VarChar);
                para[0].Value = objData.SKU;
                para[1] = new SqlParameter(busDocumentProduct.DOCUMENTID, SqlDbType.Int);
                para[1].Value = objData.DocumentID;
                result = clsSQL.SPExecute("DocumentProduct_OrderDown", para);
            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoDocumentProduct", " OrderDown(DocumentProduct objData)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            return (result > 0);
        }
        public bool OrderUp(DocumentProduct objData)
        {
            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[2];
            try
            {
                para[0] = new SqlParameter(busDocumentProduct.SKU, SqlDbType.VarChar);
                para[0].Value = objData.SKU;
                para[1] = new SqlParameter(busDocumentProduct.DOCUMENTID, SqlDbType.Int);
                para[1].Value = objData.DocumentID;
                result = clsSQL.SPExecute("DocumentProduct_OrderUp", para);
            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoDocumentProduct", "OrderUp(DocumentProduct objData)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            return (result > 0);
        }
        public bool Delete(DocumentProduct objData)
        {
            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[2];
            try
            {
                para[0] = new SqlParameter(busDocumentProduct.SKU, SqlDbType.VarChar);
                para[0].Value = objData.SKU;
                para[1] = new SqlParameter(busDocumentProduct.DOCUMENTID, SqlDbType.Int);
                para[1].Value = objData.DocumentID;
                result = clsSQL.SPExecute("DocumentProduct_Delete", para);
            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoDocumentProduct", "Delete(DocumentProduct objData)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            return (result>0);
        }
    }
}


